﻿#set($PKColumnName="")
#foreach($u in $TableInfos)
#beforeall
#each
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK=="True")
#set($PKColumnName=  $c.ColumnName)
#end
#end
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BrainSoft.Common;
using BrainSoft.Entity;
using BrainSoft.Models;
using BrainSoft.Models.Interfaces;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace BrainSoft.Web.Manager.SystemMain.${u.TabName}
{
    public partial class Default : System.Web.UI.Page
    {
        // 返回json字符串
        protected string returnJson = string.Empty;
        string CMD = (string)Utils.GetQueryAndFormString("CMD", MethodType.Get, 10, 1, DataType.CharAndNum);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                switch (CMD)
                {
                    case "List":
                        List();
                        break;
                    case "Delete":
                        string dId = (string)Utils.GetQueryAndFormString("DId", MethodType.Get, 255, 0,DataType.Str);
                        DeleteList(dId);
                        break;
                }
            }
        }


        /// <summary>
        /// 批量删除${u.TabCaption}
        /// </summary>
        /// <param name="idList">id列表 234,4,5</param>
        /// <returns>json</returns>
        public void DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new BrainSoft.Common.RegExpression.NumAndBarRegExpression();
             returnJson = string.Empty;
            if (regex.IsMatch(actionId))
            {
                IDataService<${u.TabName}Entity> ${u.TabName}Sv = ServiceBuilder.Build${u.TabName}Service();
                if (actionId.IndexOf('_') > -1)
                {
                    if (${u.TabName}Sv.DeleteList(actionId))
                    {
                        returnJson = "{\"success\":\"true\"}";
                    }
                    else
                        returnJson = "{\"success\":\"false\"}";
                }
                else
                {
                    ${u.TabName}Entity ${u.TabName} = new ${u.TabName}Entity();
                    int recordNum = 0;
                    ${u.TabName}.${PKColumnName} = Utils.StringToInt(actionId, recordNum);
                   
                    if ( ${u.TabName}Sv.Delete(${u.TabName}))
                    {
                        returnJson = "{\"success\":\"true\"}";
                    }
                    else
                        returnJson = "{\"success\":\"false\"}";
                }
            }
            else
            {
                returnJson = "{\"success\":\"false\",\"message\":\"参数类型不正确\"}";
            }
            Response.Write(returnJson);
            Response.End();
        }

        /// <summary>
        /// 返回${u.TabCaption}列表
        /// </summary>
        /// <returns>json</returns>
        public void List()
        {
            int recordNum = 0;
            QueryParam qp = new QueryParam();
            qp.Orderfld = "${PKColumnName} desc ";
            int pageSize = (int)Utils.GetQueryAndFormString("limit",MethodType.Get, 3, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", MethodType.Get, 9, 1, DataType.Int);
            if (pageIndex < 1)
                pageIndex = 1;
            qp.PageIndex = pageIndex ;
            qp.PageSize = pageSize;
            IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<${u.TabName}Entity> ${u.TabName}Sv = ServiceBuilder.Build${u.TabName}Service();
            List<${u.TabName}Entity> ${u.TabName}List = ${u.TabName}Sv.List(qp, out recordNum);
            returnJson = string.Concat("{\"totalPorperty\":", recordNum, ",\"root\":", JsonConvert.SerializeObject(${u.TabName}List, Formatting.Indented, jsDataConvert), "}");
            Response.Write(returnJson);
            Response.End();
        }
    }
}
#end